{include file="public/header"}
<body>
	<div class="sectionY">
		<p class="p1">购物车</p>
		{if $is_login eq 1}
		<a href="javascript:;" class="a3 text_sli_btn">编辑</a>
		{/if}
	</div>
	<div class="kong1"></div>
	{if $is_login eq 1}
	<div class="car_section1">
		{foreach $list.data as $item}
		<div class="a1 clearfix">
			<div class="list">
				<img src="__PUBLIC__/home/images/a33.png" alt="" class="img1" data-cart_id="{$item.cart_id}" data-cart_price="{$item.price}">
				<img src="__PUBLIC__/home/images/a34.png" alt="" class="img2" data-cart_id="{$item.cart_id}" data-cart_price="{$item.price}">
			</div>
			<div class="logo">
				<img src="{$item.img_url}" alt="">
			</div>
			<div class="right clearfix">
				<p class="p1 chao2">{$item.title}</p>
				<div class="e1">
					<div class="a1_1">
						<span class="s1">￥{$item.price}</span>
					</div>
					<div class="a1_2">
						<img src="__PUBLIC__/home/images/a36.png" alt="" class="im1 del_btn" data-cart_id="{$item.cart_id}">
						<span class="s1 cart_num">{$item.cart_num}</span>
						<img src="__PUBLIC__/home/images/a37.png" alt="" class="im2 add_btn" data-cart_id="{$item.cart_id}">
					</div>
				</div>
			</div>
		</div>
		{/foreach}
	</div>
	<div class="kong"></div>
	<div class="foot_c">
		<div class="left">
			<img src="__PUBLIC__/home/images/a33.png" alt="" class="img_all">
			<img src="__PUBLIC__/home/images/a34.png" alt="" class="img_all1">
			<span class="s1">全选</span>
		</div>
		<div class="center">
			<p class="p1">合计：<span class="s1 total_price">￥0.00</span></p>
		</div>
		<div class="last">
			<a href="javascript:;" id="text_sli" data-submit_status="1" class="goOrder">去结算</a>
			<a href="javascript:;" id="text_sli1" class="delCart" style="display: none">删除</a>
		</div>
	</div>
	{else}
	<div style="text-align: center; padding: 20px 0px;">
		<img src="__PUBLIC__/home/images/cart_empty.png" alt="" style="display: block; width: 40%; margin: auto;">
		<a href="{:url('Index/login')}" style="display:inline-block;padding: 10px 40px;background-color: #ff4546;border-radius: 25px;color: #fff;border: none; margin-top: 20px;">去登录</a>
	</div>
	{/if}
{include file="public/tabbar"}
</body>
<script>
	$(function(){
		$(".del_btn").click(function(){
			var text = $(this).siblings(".s1").text(), cart_id = $(this).data("cart_id");
			var rs = addDelete(0, cart_id, text, this);
		})
		$(".add_btn").click(function(){
			var text = $(this).siblings(".s1").text(), cart_id = $(this).data("cart_id");
			addDelete(1, cart_id, text, this);
		})

		$(".img1").click(function(){
			$(this).hide();
			$(this).siblings(".img2").show();
			calcute();
		})
		$(".img2").click(function(){
			$(this).hide();
			$(this).siblings(".img1").show();
			calcute();
		})
		$(".img_all").click(function(){
			$(this).hide().siblings(".img_all1").show()
			$(".img2").show();
			$(".img1").hide();
			calcute();
		})
		$(".img_all1").click(function(){
			$(this).hide().siblings(".img_all").show()
			$(".img2").hide();
			$(".img1").show();
			calcute();
		})
		$(".text_sli_btn").click(function(){
			let txt = $(this).text();
			$("#text_sli, #text_sli1").toggle();
			if(txt == "编辑"){
				$(this).text("保存");
			}else{
				$(this).text("编辑");
			}
		})
		$(".goOrder").click(function () {
			let selected = $(".car_section1").find(".list .img2:visible"), submit_status = $(this).data("submit_status");
			if(selected.length == 0){
				modal.msg("请选择完商品再提交");
				return ;
			}
			if(submit_status == 0){
				return ;
			}
			$(this).css("background", "#aaa");
			//#ff4546
			$(this).data("submit_status", 0);
			let cart_id = new Array();
			$.each(selected, function (i,v) {
				cart_id.push($(v).data("cart_id"));
			});
			location.href="{:url('Order/preOrder')}?cart_id="+cart_id.join(",");
		});
		$(".delCart").click(function () {
			let selected = $(".car_section1").find(".list .img2:visible");
			if(selected.length==0){
				modal.msg("您还未选择待删除的商品");
				return true;
			}else{
				modal.alert("确定删除选择商品？", "确定", function () {
					delCart();
				});
			}
		});
	})

	//增加/减少
	function addDelete(type, cart_id, text, o) {
		$.post("{:url('Cart/addDelete')}", {type:type, cart_id:cart_id}, function (res) {
			modal.msg(res.result_info);
			if(res.result_code == 0){
				if(type == 1){
					text++;
				}else{
					text--;
				}
				$(o).siblings(".s1").text(text);
				calcute();
			}
		});
	}

	//计算商品金额
	function calcute() {
		let selected = $(".car_section1").find(".list .img2:visible");
		let total = 0;
		$.each(selected, function (i, v) {
			let num = parseInt($(v).parents(".a1").find(".cart_num").text()), price = parseFloat($(v).data("cart_price"));
			total += num*price;
		});
		$(".total_price").text("￥"+total);
	}
	
	
	
	//删除商品
	function delCart() {
		let selected = $(".car_section1").find(".list .img2:visible");
		let data_arr = new Array();
		$.each(selected, function (i, v) {
			data_arr.push($(v).data("cart_id"));
		});
		$.post("{:url('cartRemove')}", {cart_id:data_arr.join(",")}, function (res) {
			modal.msg(res.result_info);
			if(res.result_code == 0){
				selected.parent().parent().remove();
			}
			calcute();
		});
	}
</script>
</html>